package com.ruoyi.teacher.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.help.domain.StudentAfter;
import com.ruoyi.help.mapper.StudentAfterMapper;
import com.ruoyi.help.service.IStudentAfterService;
import com.ruoyi.help.vo.StudentAfterVo;
import com.ruoyi.student.domain.BblClass;
import com.ruoyi.student.domain.Student;
import com.ruoyi.student.mapper.BblClassMapper;
import com.ruoyi.student.mapper.StudentMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.dto.StudentAfterDto;
import com.ruoyi.teacher.dto.TeachingDto;
import com.ruoyi.teacher.mapper.TeacherMapper;
import com.ruoyi.teaching.domain.TeacherStalke;
import com.ruoyi.teaching.mapper.TeacherStalkeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teacher.mapper.DailyPaperMapper;
import com.ruoyi.teacher.domain.DailyPaper;
import com.ruoyi.teacher.service.IDailyPaperService;

/**
 * 日报Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-19
 */
@Service
public class DailyPaperServiceImpl implements IDailyPaperService {
    @Autowired
    private DailyPaperMapper dailyPaperMapper;

    @Autowired
    private StudentAfterMapper studentAfterMapper;

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherStalkeMapper teachingTrackMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private BblClassMapper classMapper;

    /**
     * 查询日报
     *
     * @param id 日报主键
     * @return 日报
     */
    @Override
    public DailyPaper selectDailyPaperById(Long id) {
        return dailyPaperMapper.selectDailyPaperById(id);
    }

    /**
     * 查询日报列表
     *
     * @param dailyPaper 日报
     * @return 日报
     */
    @Override
    public List<DailyPaper> selectDailyPaperList(DailyPaper dailyPaper) {
        return dailyPaperMapper.selectDailyPaperList(dailyPaper);
    }

    /**
     * 新增日报
     *
     * @param dailyPaper 日报
     * @return 结果
     */
    @Override
    public int insertDailyPaper(DailyPaper dailyPaper) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        // 获取当前登录老师信息
        Teacher teacher = teacherMapper.selectTeacherById(userId);
        dailyPaper.setTeacherId(teacher.getId());
        dailyPaper.setCreateTime(new Date());
        dailyPaper.setContent("[日报]：" + dailyPaper.getContent());
        int i = dailyPaperMapper.insertDailyPaper(dailyPaper);
        // 添加就业跟踪记录
        addStudentAfter(dailyPaper, teacher);
        // 添加教学跟踪记录
        addTeacherSralke(dailyPaper, userId, teacher);
        return i;
    }

    // 添加就业跟踪记录
    private void addStudentAfter(DailyPaper dailyPaper, Teacher teacher) {
        // 取出日报中的就业跟踪记录
        List<StudentAfterDto> studentTracks = dailyPaper.getStudentTracks();
        // 循环添加就业跟踪记录
        if (studentTracks.size() > 0) {
            StudentAfter studentAfter = new StudentAfter();
            for (StudentAfterDto track : studentTracks) {
                // 设置字段
                studentAfter.setAfterNote(track.getAfterNote());
                studentAfter.setStuName(track.getStuName());
                studentAfter.setAfterTime(track.getAfterTime());
                studentAfter.setStuType(track.getStuType());
                studentAfter.setAfterGoal(track.getAfterGoal());
                BblClass bblClass = classMapper.selectBblClassById(track.getClassId());
                studentAfter.setClassName(bblClass.getName());
                studentAfter.setTeacherName(teacher.getName());
                // 添加到数据库
                studentAfterMapper.insertStudentAfter(studentAfter);
            }
            List<StudentAfter> list = dailyPaperMapper.selectStudentAfterListByStuName(studentAfter.getStuName());
            if (list.size() > 0) {
                for (StudentAfter after : list) {
                    List<Integer> afterCount = new ArrayList<>();
                    afterCount.add(after.getAfterCount());
                    Optional<Integer> max = afterCount.stream().max(Integer::compareTo);
                    Integer maxCount = max.get();
                    studentAfter.setAfterCount(maxCount + 1);
                    studentAfterMapper.updateStudentAfter(studentAfter);
                }
            }
        }
    }

    // 添加教学跟踪记录
    private void addTeacherSralke(DailyPaper dailyPaper, Long userId, Teacher teacher) {
        List<TeacherStalke> teachingTracks = dailyPaper.getTeachingTracks();
        if (teachingTracks != null) {
            TeacherStalke teacherStalke = new TeacherStalke();
            for (TeacherStalke track : teachingTracks) {
                Student student = studentMapper.selectStudentById(track.getStudentId());
                String name = student.getName();
                teacherStalke.setTeachername(teacher.getName());
                teacherStalke.setStudentId(track.getStudentId());
                teacherStalke.setTeacherId(teacher.getId());
                teacherStalke.setStudentsname(name);
                teacherStalke.setClassId(track.getClassId());
                teacherStalke.setRemark(track.getRemark());
                teacherStalke.setCreationtime(track.getCreationtime());
                teacherStalke.setDiscount(track.getDiscount());
                teacherStalke.setImpartType(track.getImpartType());
                teacherStalke.setTeacherId(userId);
                teachingTrackMapper.insertTeacherStalke(teacherStalke);
            }
            List<TeacherStalke> ts = dailyPaperMapper.selectTeacherStalkeListById(teacherStalke.getStudentsname());
            if (ts.size() > 0) {
                for (TeacherStalke stalke : ts) {
                    List<Long> afterCount = new ArrayList<>();
                    if (stalke.getStalkeCount() == 0 || stalke.getStalkeCount() == null) {
                        stalke.setStalkeCount(0L);
                    }
                    afterCount.add(stalke.getStalkeCount());
                    Optional<Long> max = afterCount.stream().max(Long::compareTo);
                    Long maxCount = max.get();
                    stalke.setStalkeCount(maxCount + 1);
                    teachingTrackMapper.updateTeacherStalke(stalke);
                }
            }
        }
    }

    /**
     * 修改日报
     *
     * @param dailyPaper 日报
     * @return 结果
     */
    @Override
    public int updateDailyPaper(DailyPaper dailyPaper) {
        return dailyPaperMapper.updateDailyPaper(dailyPaper);
    }

    /**
     * 批量删除日报
     *
     * @param ids 需要删除的日报主键
     * @return 结果
     */
    @Override
    public int deleteDailyPaperByIds(Long[] ids) {
        return dailyPaperMapper.deleteDailyPaperByIds(ids);
    }

    /**
     * 删除日报信息
     *
     * @param id 日报主键
     * @return 结果
     */
    @Override
    public int deleteDailyPaperById(Long id) {
        return dailyPaperMapper.deleteDailyPaperById(id);
    }
}
